<?php
@session_start();

require_once ("includes/connect.php");
require_once ("includes/function.php");

$pet_return_id = $_REQUEST["pet_return_id"];

$db = new myDBS();
$Cur = new Currency;

//$sql = "SELECT tbl_promise_borrow.*, pet_id "
//        . " FROM tbl_pet_borrow, tbl_promise_borrow  "
//        . " WHERE pet_id  = '$pet_id' "
//        . " AND tbl_pet_borrow.borrow_id = tbl_promise_borrow.borrow_id ";
$sql = " SELECT pet_return_id, return_amount, expenses_lvl2.e_id, expenses2_name, expenses3_name, "
        . " reason, card_id1, card_id2, card_id3, total, remain "
        . ", panngan2.panngan2_id, panngan2_name, panngan3.panngan3_id, panngan3_name, budget_year "
        . " FROM tbl_promise_budget "
        . " LEFT JOIN expenses_lvl2 ON ex2_id = expenses_lvl2.expenses2_id "
        . " LEFT JOIN expenses_lvl3 ON ex3_id = expenses_lvl3.expenses3_id "
        . " JOIN panngan2 ON tbl_promise_budget.panngan2_id = panngan2.panngan2_id "
        . " JOIN panngan3 ON tbl_promise_budget.panngan3_id = panngan3.panngan3_id "
        . " JOIN tbl_return ON tbl_return.promise_id = tbl_promise_budget.id "
        . " WHERE pet_return_id =  '$pet_return_id' ";

//echo $sql;
$result = $db->runSQL($sql);
$rows = $db->fetch_object($result);

// just require TCPDF instead of FPDF
//define('FPDF_FONTPATH','fpdf/font/');
require_once('fpdf/TCPDF/tcpdf.php');
require_once('fpdf/FPDI/fpdi.php');

class PDF extends FPDI {

    /**
     * "Remembers" the template id of the imported page
     */
    var $_tplIdx;

    /**
     * include a background template for every page
     */
    function Header() {
        if (is_null($this->_tplIdx)) {
            $this->numPages = $this->setSourceFile('form/form2_1.pdf');
            //$this->numPages = $this->setSourceFile($fullPathToFile);
            $this->_tplIdx = $this->importPage(1);
        }
        $this->useTemplate($this->_tplIdx);

        /*
          //$this->AddFont('THSarabunNew','','THSarabunNew.php');
          //$this->SetFont('THSarabunNew', '', 9);
          $this->SetTextColor(255);
          $this->SetXY(60.5, 24.8);
          $this->Cell(0, 8.6, "TCPDF and FPDI");
         */
    }

    function Footer() {
        
    }

}

// initiate PDF
$pdf = new PDF($orientation = 'P', $unit = 'mm', 'A4', $unicode = true, $encoding = 'UTF-8', $diskcache = false, $pdfa = false);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
//$pdf->SetAutoPageBreak(true, 40);
$pdf->setFontSubsetting(false);

// add a page
$pdf->AddPage();

$vertical_align = 2;

//$pdf->AddFont('THSarabunNew','','THSarabunNew.php');
//$pdf->SetFont("freeserif", "", 12);
$pdf->SetFont('thsarabunb', 'B', 15);
// now write some text above the imported page
$pdf->SetXY(32, 27 + $vertical_align);
//$pdf->Write(0, $_SESSION["lvl1_id"]);
$pdf->Write(0, "สำนักงานเทศบาลนครอุดรธานี");
//
$pdf->SetXY(33, 34 + $vertical_align);
$pdf->Write(0, $rows->budget_year);
//
$pdf->SetXY(92, 34 + $vertical_align);
$pdf->Write(0, $rows->pet_return_id);
//
$pdf->SetXY(26, 41.5 + $vertical_align);
$pdf->Write(0, $rows->panngan2_name);
//
$elass = gen_account_id($rows->panngan2_id, $rows->panngan3_id, $rows->e_id);
$pdf->SetXY(155, 48.5 + $vertical_align);
$pdf->Write(0, $elass);
//
$pdf->SetXY(84, 41 + $vertical_align);
$pdf->Write(0, $rows->panngan3_name);
//
$pdf->SetXY(31, 48.5 + $vertical_align);
$pdf->MultiCell(50, 10, $rows->expenses2_name, 0, '', 0, 0, '', '', true);
////
$pdf->SetXY(90, 47 + $vertical_align);
$pdf->Write(0, $rows->expenses3_name);
////
////$pdf->SetXY(90, 55 + $vertical_align);
////$pdf->Write(0, $rows->expenses3_name);

$pdf->SetXY(76, 69 + $vertical_align);
$pdf->Cell(41, 0, number_format($rows->return_amount, 2), 0, 0, 'R');
//
////$pdf->SetXY(120, 69 + $vertical_align);
////$pdf->MultiCell(90, 45, $_SESSION["lvl2"] . " ประจำ " . $rows->month_year, 0, '', 0, 1, '', '', true);
//
//

$pdf->SetXY(76, 76 + $vertical_align);
$pdf->Cell(41, 0, number_format(0, 2), 0, 0, 'R');

$pdf->SetXY(76, 83 + $vertical_align);
$pdf->Cell(41, 0, number_format($rows->return_amount, 2), 0, 0, 'R');

$pdf->SetXY(76, 90 + $vertical_align);
$pdf->Cell(41, 0, number_format(0, 2), 0, 0, 'R');

$pdf->SetXY(76, 97 + $vertical_align);
$pdf->Cell(41, 0, number_format($rows->return_amount, 2), 0, 0, 'R');

//$pdf->SetXY(76, 105 + $vertical_align);
//$pdf->Cell(41, 0, number_format($rows->return_amount, 2), 0, 0, 'R');
//
$pdf->SetXY(117, 69 + $vertical_align);
$c = "ส่งใช้เงินยืม". $rows->reason . " \nให้แก่ 1. ". get_name_person($rows->card_id1);
if ($rows->card_id2) {
    $c = $c . " 2. ". get_name_person($rows->card_id2);
}
if ($rows->card_id3) {
    $c = $c . " 3. ". get_name_person($rows->card_id3);
}

$c = $c . "\n เงินยืม ". number_format($rows->total, 2);
$c = $c . "\n จ่ายจริง ". number_format($rows->return_amount, 2);
$c = $c . "\n คืนคลัง ". number_format($rows->remain, 2);
$pdf->MultiCell(83, 42, $c , 0, '', 0, 1, '', '', true);

$pdf->SetXY(58, 112.5 + $vertical_align);
$pdf->Write(0, $Cur->bahtThai($rows->return_amount));
//
//
$pdf->SetXY(22, 168 + $vertical_align);
$pdf->Cell(39, 0, number_format($rows->return_amount, 2), 0, 0, 'C');

$pdf->SetXY(90, 168 + $vertical_align);
$pdf->Cell(39, 0, number_format($rows->return_amount, 2), 0, 0, 'C');

$pdf->SetXY(157, 168 + $vertical_align);
$pdf->Cell(39, 0, number_format($rows->return_amount, 2), 0, 0, 'C');

$pdf->SetXY(29, 225 + $vertical_align);
$pdf->Cell(36, 0, number_format($rows->return_amount, 2), 0, 0, 'C');

$pdf->SetXY(73, 225 + $vertical_align);
$pdf->Write(0, $Cur->bahtThai($rows->return_amount));


$pdf->Output('output_form1.pdf', 'I');
?>